<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\App;
use think\facade\View;

abstract class Base
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
        $this->initialize();
    }

    protected function initialize()
    {
        global $_G;
        if (empty($_G['uid']) || empty($_G['user'])) {
            return redirect('admin/account/login');
        }
        if ($_G['user']['type'] !== 'system') {
            $_G['user']->logout();
            return redirect('index/index/index');
        }
        $this->admin_menu();
    }

    private function admin_menu()
    {
        global $_G;
        $_G['admin_menu'] = [
            'Index'    => [
                'name'     => '控制台',
                'icon'     => 'icon-home',
                'sub_menu' => [
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '数据统计',
                        'active_list' => ['index'],
                        'url'         => 'admin/index/index',
                    ],
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '访问前台',
                        'active_list' => [],
                        'url'         => 'index/index/index',
                        'target'      => '_blank',
                    ],
                ],
            ],
//            'Setting'  => [
//                'name'     => '设置',
//                'icon'     => 'icon-setting',
//                'sub_menu' => [
//                    [
//                        'icon'        => 'icon-nav',
//                        'name'        => '基础设置',
//                        'active_list' => ['index'],
//                        'url'         => 'admin/setting/index',
//                    ],
//                    [
//                        'icon'        => 'icon-nav',
//                        'name'        => '注册与访问',
//                        'active_list' => ['account'],
//                        'url'         => 'admin/setting/account',
//                    ],
////                    [
////                        'icon'        => 'icon-nav',
////                        'name'        => '裂变推广',
////                        'active_list' => ['fission'],
////                        'url'         => 'admin/setting/fission',
////                    ],
//                ],
//            ],
            'User'     => [
                'name'     => '用户',
                'icon'     => 'icon-users',
                'sub_menu' => [
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '用户列表',
                        'active_list' => ['index', 'add_user', 'edit_user', 'batch_add'],
                        'url'         => 'admin/user/index',
                    ],
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '导出用户',
                        'active_list' => ['export_user'],
                        'url'         => 'admin/user/export_user',
                    ],
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '搜索用户',
                        'active_list' => ['search_user'],
                        'url'         => 'admin/user/search_user',
                    ],
                ],
            ],
            'Portal'   => [
                'name'     => '内容',
                'icon'     => 'icon-article',
                'sub_menu' => [
//                    [
//                        'icon'        => 'icon-nav',
//                        'name'        => '文章管理',
//                        'active_list' => ['index', 'add_article', 'edit_article', 'delete_article'],
//                        'url'         => 'admin/portal/index',
//                    ],
//                    [
//                        'icon'        => 'icon-nav',
//                        'name'        => '用户分享',
//                        'active_list' => ['share', 'add_share', 'edit_share', 'delete_share'],
//                        'url'         => 'admin/portal/share',
//                    ],
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '播放记录',
                        'active_list' => ['download_log', 'add_download_log', 'edit_download_log', 'delete_download_log'],
                        'url'         => 'admin/portal/download_log',
                    ],
                    [
                        'icon'        => 'icon-nav',
                        'name'        => 'cookie设置',
                        'active_list' => ['cookie_set'],
                        'url'         => 'admin/portal/cookie_set',
                    ],
                ],
            ],
            'Announce' => [
                'name'     => '运营',
                'icon'     => 'icon-circle',
                'sub_menu' => [
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '充值卡',
                        'active_list' => ['card', 'card_add', 'card_edit', 'card_delete', 'card_search', 'card_export'],
                        'url'         => 'admin/announce/card',
                    ],
                ],
            ],
            'Tools'    => [
                'name'     => '工具',
                'icon'     => 'icon-tools',
                'sub_menu' => [
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '更新缓存',
                        'active_list' => ['index'],
                        'url'         => 'admin/tools/index',
                    ],
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '数据备份',
                        'active_list' => ['database'],
                        'url'         => 'admin/tools/database',
                    ],
                    [
                        'icon'        => 'icon-nav',
                        'name'        => '数据恢复',
                        'active_list' => ['backup'],
                        'url'         => 'admin/tools/backup',
                    ]
                ],
            ],
        ];
        View::assign('_G', $_G);
    }
}
